var optDeptId = null, userTable = null, depts = [];
// 配置
var id = {
    table: 'base_dept_user_table',
};
var base_dept_user = {
    table: {
        id: id.table,
        url: api.base.dept_user,
        cols:[
            {fixed: 'left',field: 'trueName', title: '姓名',minWidth: 120},
            {field: 'loginName', title: '登录名',minWidth: 120},
            {field: 'avatar', title: '头像', templet: function (obj) {
                    let avatar = obj.avatar;
                    if(avatar == ''){
                        if(obj.sex == 1){
                            avatar = '/assets/images/sex-1.png';
                        }else{
                            avatar = '/assets/images/sex-0.png';
                        }
                    }
                    return '<img src="' + avatar + '" width="32" height="32">'
                }},
            {field: 'phone', title: '电话',minWidth: 120},
            {field: 'nickName', title: '昵称',minWidth: 120},
            {field: 'sex', title: '性别', templet: function (obj) {
                    return obj.sex == 1 ? '男' : '女';
                }},
            {field: 'email', title: '电子邮件',minWidth: 120},
            {field: 'id',fixed: 'right', title: '操作', minWidth: 80, toolbar: page_auth.right()}
        ],
        toolbar: page_auth.top(),
        bar: function(bar){
            switch (bar.event) {
                case 'create':
                    base_dept_view.form();
                    break;
            }
        },
        tool: function (tool) {
            switch (tool.event) {
                case 'delete':
                    base_dept_view.delete(tool.data.id)
                    break;
            }
        },
    }
}
initialize({
    module:['dtree'],
    title: '部门管理',
    init: function () {
        userTable = soda.table.render(base_dept_user.table);
        base_dept_view.tree();
    }
})
var base_dept_view = {
    form: function(){
        if(optDeptId == null){
            soda.tips.warning('请先选择一个部门');
            return false;
        }
        app.page({
            title: '用户分配',
            url: '/components/base/dept_user.html',
            height: '420px',
            finish: function () {
                table.reload(id.table, {});
            },
            init: function () {
                let userVm = $('#dept-user-query-table').vm({items:[]});
            },
            opt: function () {
                
            }
        })
    },
    tree: function(){
        base_dept_data.dept.get(function(d){
            depts = d.list;
            dtree.render({
                elem: '#dept-tree',
                data: d.tree,
                toolbar:true,
                record:true,
                initLevel: 10,
                toolbarScroll:"#dept-tree-toolbar",
                dot:false,
                skin: "layui",  // layui主题风格
                toolbarBtn:[
                    [
                        {"label":"排序", "name":"orderNumber", "type":"text", "value": "0", "placeholder": "数字越大越靠前面"}
                    ],
                    [
                        {"label":"排序", "name":"orderNumber", "type":"text"}
                    ]
                ],
                toolbarFun: {
                    addTreeNode: function(treeNode, $div){
                        base_dept_data.dept.create(treeNode, function(){
                            base_dept_view.tree();
                        });
                    },
                    editTreeLoad: function(treeNode){
                        let node = soda.json.get(depts, treeNode.nodeId);
                        $('input[name="orderNumber"]').val(node.orderNumber);
                    },
                    editTreeNode: function(treeNode, $div){
                        base_dept_data.dept.update(treeNode, function(){
                            base_dept_view.tree();
                        });
                    },
                    delTreeNode: function(treeNode, $div){
                        base_dept_data.dept.delete(treeNode, function(){
                            base_dept_view.tree();
                        });
                    }
                }
            });
            dtree.on("node('dept-tree')" ,function(obj){
                optDeptId = obj.param.nodeId;
                render.dept.user.init();
            });
        });
    },
    delete: function(_id){
        soda.tips.confirm({
            text: '确定要从部门里面移除这个账号吗?',
            confirm: function () {
                base_dept_data.user.delete(_id, function(){
                    render.dept.user.query();
                });
            }
        });
    }
}
var render = {
    dept: {
        user: {
            init: function(){
                userTable.reload({
                    where: {
                        page: 1,
                        deptId: optDeptId
                    }
                })
            },
            query: function(){
                userTable.reload({
                    where: {
                        deptId: optDeptId
                    }
                })
            }
        }
    }
}
var opt = {
    query: function(){
        let key = $('input[name="user-query"]').val();
        key = $.trim(key);
        if(key.length < 2){
            soda.tips.error('至少要输入2个字符');
            return false;
        }
        base_dept_data.user.query(key, function(d){
            let userVm = $('#dept-user-query-table').vm({items:[]});
            if(d.length > 0){
                let data = [];
                $.each(d, function(i, obj){
                    if(obj.sex == ''){
                        obj.sex = -1;
                    }
                    if(obj.avatar == ''){
                        if(obj.sex == 1){
                            obj.avatar = '/assets/images/sex-1.png';
                        }
                        if(obj.sex == -1){
                            obj.avatar = '/assets/images/sex-2.png';
                        }
                        if(obj.sex == 0){
                            obj.avatar = '/assets/images/sex-0.png';
                        }
                    }
                    data.push(obj);
                })
                userVm.set({items: data})
            }
        });
    },
    select: function(_obj){
        let userId = $(_obj).attr('data-id');
        let form = {
            userId: userId,
            departmentId: optDeptId
        }
        base_dept_data.user.add(form, function(){
            render.dept.user.query();
        })
    }
}

var base_dept_data = {
    dept: {
        get: function(_callback){
            let param = {
                url: api.base.dept,
                tips: 0,
                callback: _callback
            }
            return soda.http.get(param);
        },
        create: function(_treeNode, _callback){
            _treeNode.orderNumber = soda.convert.s2n(_treeNode.orderNumber); 
            let node = {fullName: _treeNode.context, parentId: _treeNode.parentId, orderNumber: _treeNode.orderNumber};
            let param = {
                url: api.base.dept,
                data: node,
                callback: _callback
            }
            return soda.http.post(param);
        },
        update: function(_treeNode, _callback){
            let id = _treeNode.nodeId;
            _treeNode.orderNumber = soda.convert.s2n(_treeNode.orderNumber); 
            let node = {fullName: _treeNode.context, orderNumber: _treeNode.orderNumber};
            let param = {
                url: api.base.dept + '/' + id,
                data: node,
                callback: _callback
            }
            return soda.http.put(param);
        },
        delete: function(_treeNode, _callback){
            let id = _treeNode.nodeId;
            let param = {
                url: api.base.dept + '/' + id,
                callback: _callback
            }
            return soda.http.delete(param);
        }
    },
    user: {
        query: function(_key, _callback){
            let param = {
                url: api.base.dept_user + '-query/' + _key,
                tips: 0,
                callback: _callback
            }
            return soda.http.get(param);
        },
        add: function(_form, _callback){
            let param = {
                url: api.base.dept_user,
                data: _form,
                callback: _callback
            }
            return soda.http.post(param);
        },
        delete: function(_id, _callback){
            let param = {
                url: api.base.dept_user + '/' + _id + '-' + optDeptId,
                callback: _callback
            }
            return soda.http.delete(param);
        }
    }
}

